package com.demo.core.dao;

import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.demo.base.model.PublicNotice;
import com.demo.base.page.IPagableDTO;
import com.demo.base.page.PagableDTO;

/**
 * Created by jiandandia on 2017/4/5.
 */
@Repository
public class PublicMessageDao extends BaseDao{


    public PublicNotice saveOrUpdatePublicNotice(PublicNotice notice) throws Exception{
        return saveEntity(notice);
    }


    public boolean addBrowsingNumber(String noticeId) {
        try {
            String hql = "update PublicNotice set browsingNumber=browsingNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean addFollowersNumber(String noticeId) {
        try {
            String hql = "update PublicNotice set followersNumber=followersNumber + 1 "
                    + " where id=?";
            hExecuteUpdate(hql, new Object[]{noticeId});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void closePublicNotice(String id) throws Exception{
        Session session = getSession();
        PublicNotice retval = null;
        try {
            retval = (PublicNotice) session.get(PublicNotice.class, id);
            retval.setStatus(PublicNotice.STATUS_CLOSE_INT);
            retval = (PublicNotice) session.merge(retval);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }

    public IPagableDTO<PublicNotice> queryPublicNotices(String queryVal, IPagableDTO pagableDTO){
        Session s = getSession();
        IPagableDTO<PublicNotice> retval = new PagableDTO<PublicNotice>();
        try {
            Criteria c = s.createCriteria(PublicNotice.class);
            if (StringUtils.isNotEmpty(queryVal)) {
                c.add(Restrictions.like(PublicNotice.TITLE, "%" + queryVal + "%"));
            }
            Long totalRow = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
            retval.setTotalRow(totalRow.intValue());
            c.setProjection(null);
            if (null != pagableDTO) {
                c.setFirstResult(pagableDTO.getStartRow());
                c.setFetchSize(pagableDTO.getPageSize());
                c.setMaxResults(pagableDTO.getPageSize());
            }
            retval.setData(c.list());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retval;
    }

    public void deltePublicNoticesById(String id) throws Exception{
        Session s = getSession();
        try {
            s.delete(s.get(PublicNotice.class, id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }
}
